unit selecttest;

interface

uses Classes, Windows, SqlExpr, TestBase;

type

  TSelectTest = class(TAbstractTest)
  private
    FSQL: TStrings;
    FAttempts: Integer;
    FReadFieldValues: Boolean;
    FUnprepareOnClose: Boolean;
    FValueReadCount: Integer;
    function RunFor(Connection: TSQLConnection): DWORD;
  protected
    function GetName: string; override;
    function GetDescription: string; override;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Configure; override;
    procedure Run; override;
    property SQL: TStrings read FSQL;
    property Attempts: Integer read FAttempts write FAttempts;
    property ValueReadCount: Integer read FValueReadCount write FValueReadCount;
    property ReadFieldValues: Boolean read FReadFieldValues write FReadFieldValues;
    property UnprepareOnClose: Boolean read FUnprepareOnClose write FUnprepareOnClose;
  end;

implementation

uses SysUtils, IniFiles, DB, DBXpress, Forms, Data, SelectTestConfig;

{ TSelectTest }

constructor TSelectTest.Create;
begin
  inherited;
  FSQL := TStringList.Create;
  FSQL.LoadFromFile(ExtractFilePath(Application.ExeName) + '\select.sql');
  FAttempts := IniFile.ReadInteger('select test', 'loopcount', 10);
  FReadFieldValues := IniFile.ReadBool('select test', 'readfieldvalues', False);
  FValueReadCount := IniFile.ReadInteger('select test', 'valuereadcount', 1);
  FUnprepareOnClose := IniFile.ReadBool('select test', 'unprepareonclose', False);
end;

destructor TSelectTest.Destroy;
begin
  FSQL.SaveToFile(ExtractFilePath(Application.ExeName) + '\select.sql');
  FSQL.Free;
  IniFile.WriteInteger('select test', 'loopcount', FAttempts);
  IniFile.WriteBool('select test', 'readfieldvalues', FReadFieldValues);
  IniFile.WriteInteger('select test', 'valuereadcount', FValueReadCount);
  IniFile.WriteBool('select test', 'unprepareonclose', FUnprepareOnClose);
  inherited;
end;

function TSelectTest.GetName: string;
begin
  Result := 'Select from regress';
end;

function TSelectTest.GetDescription: string;
begin
  Result := 'Test executes select statement and fetch all data.';
end;

procedure TSelectTest.Configure;
begin
  TFormSelectTestConfig.ConfigureTest(Self);
end;

procedure TSelectTest.Run;
var
  ResultOld, ResultNew: Integer;
begin
  if DataMain.connectionOld.Tag = 1 then
  begin
    DataMain.connectionOld.Tag := 0;
    ResultOld := RunFor(DataMain.connectionOld);
    ResultNew := RunFor(DataMain.connectionNew);
  end else
  begin
    DataMain.connectionOld.Tag := 1;
    ResultNew := RunFor(DataMain.connectionNew);
    ResultOld := RunFor(DataMain.connectionOld);
  end;
  OnReceiveResults(ResultOld, ResultNew);
end;

function TSelectTest.RunFor(Connection: TSQLConnection): DWORD;
var
  I, J, K: Integer;
  Query: TSQLQuery;
  Command: ISQLCommand;
  Cursor: ISQLCursor;
begin
  Connection.Open;
  {}
  Query := TSQLQuery.Create(nil);
  try
    Query.SQLConnection := Connection;
    Query.SQL.Assign(FSQL);
    Result := GetTickCount;
    for I := 0 to FAttempts - 1 do
    begin
      Query.Open;
      while not Query.Eof do
      begin
        if FReadFieldValues then
          for K := 0 to FValueReadCount - 1 do
            for J := 0 to Query.Fields.Count - 1 do
              Query.Fields[J].AsVariant;
        Query.Next;
      end;
      Query.Close;
      if FUnprepareOnClose then
        Query.Prepared := False;
    end;
    Result := GetTickCount - Result;
  finally
    Query.Free;
  end;
  {
  Connection.SQLConnection.getSQLCommand(Command);
  Command.prepare(PChar(FSQL.Text), 0);
  Result := GetTickCount;
  for I := 0 to FAttempts - 1 do
  begin
    Command.execute(Cursor);
    while Cursor.next = 0 do ;
    Cursor := nil;
  end;
  Result := GetTickCount - Result;
  {}
end;

initialization
  TestList.Add(TSelectTest.Create);

end.
